<?php

namespace app\modules\api\controllers;

use ac\controllers\BasicController;
use app\models\ContentCategory;
use app\models\ContentCategorySearch;
use app\models\Content;
use app\models\ContentSearch;
use app\models\Tags;
use app\models\TagsRelationships;
use ac\component\tool\Tool;
use ac\component\exception\ErrMap;
use yii\helpers\Html;


class ContentController extends BasicController
{

	/*分类*/
    /*栏目列表*/
    public function actionCate()
    {
        // P($this->get);
        $search = (new ContentCategorySearch())->search($this->get);
        if($search === false){//验证搜索参数不通过
            $count = 0;
            $contentCategoryList = array();
        }else{
            if(isset($this->get['type']) and $this->get['type'] == 'ele'){
                $flag = 'ele';
            }else{
                $flag = '';
            }
            if(isset( $this->get['parentid'] )){
                $parentid = $this->get['parentid'];
            }else{
                $parentid = 0;
            }
            // P($search);
            $contentCategoryList = ContentCategory::getTreeList($search, $flag, $parentid);
        }
        // P($contentCategoryList);
        return Tool::showRes([0, '获取成功'], $contentCategoryList);
    }

	/*内容列表*/
	public function actionList()
    {
        if(!isset($this->get['catid'])){
            $count = 0;
            $pageSize = Tool::getPageSize();
            $currPage = 1;
            $contentList = array();
        }else{
            if(isset($this->get['page'])){
                $currPage = intval($this->get['page']?$this->get['page']:1);
            }else{
                $currPage = 1;
            }
            $pageSize = Tool::getPageSize();
            // P($this->get);
            $search = (new ContentSearch())->search($this->get);
            if($search === false){//验证搜索参数不通过
                $count = 0;
                $contentList = array();
            }else{
                $count = $search->count();
                $contentList = $search->select(['content_id', 'title', 'preview', 'video', 'describe', 'state', 'rejected_reason', 'member_view', 'display', 'istop', 'pc_preview_count', 'mobile_preview_count', 'add_time'])->andWhere('display = 2')
                              ->offset($pageSize*($currPage-1))->limit($pageSize)
                              ->orderBy([
                                    'istop' => SORT_DESC,
                                    'toptime' => SORT_DESC,
                                    'sort' => SORT_DESC,
                                    'add_time' => SORT_DESC
                                ])
                              // ->createCommand()->getRawSql();
                              ->asArray()->all();
                if(count($contentList)){
                    foreach($contentList as $k => $v){
                        if(!empty($v['preview'])){
                            $arr = explode(',', $v['preview']);
                            $contentList[$k]['preview'] = $arr[0];
                        }
                    }
                }

            }
        }
        // P($contentList);
        $res = array(
            'list' => $contentList,
            'pageInfo' => array(
                'count' => intval($count),
                'currPage' => $currPage,
                'pageSize' => $pageSize,
            ),
        );
        return Tool::showRes([0, '获取成功'], $res);
    }

	/*内容详情*/
	public function actionContent()
    {
    	// P($this->get);
        $catid = isset($this->get['catid'])?$this->get['catid']:'';
        $content_id = isset($this->get['id'])?$this->get['id']:'';
        if($catid or $content_id){
            if($content_id){
                $content = Content::find()
                        ->select(['content_id', 'catids', 'title', 'titlecolor', 'preview', 'video', 'istop', 'outside', 'url_target', 'outside_url', 'source', 'author', 'keyword', 'describe', 'content', 'state', 'pc_preview_count', 'mobile_preview_count', 'display', 'add_time'])
                        ->where('content_id = :cid', [':cid' => $content_id])
                        ->asArray()->one();
            }else{//说明是单页,获取次栏目最大的ID数据
                $content = Content::find()
                        ->select(['content_id', 'catids', 'title', 'titlecolor', 'preview', 'video', 'istop', 'outside', 'url_target', 'outside_url', 'source', 'author', 'keyword', 'describe', 'content', 'state', 'pc_preview_count', 'mobile_preview_count', 'display', 'add_time'])
                        ->where('catids = :cid', [':cid' => $catid])
                        ->orderBy(['content_id' => SORT_DESC])
                        ->asArray()->one();
                $content_id = $content['content_id'];
                // P($content_id);
            }
            // P($content);
            if(!is_null($content)){
                if(!empty($content['preview'])){
                    $content['preview'] = explode(',', $content['preview']);
                }else{
                    $content['preview'] = [];
                }
                if(!empty($content['content'])){
                    $content['content'] = Html::decode($content['content']);
                }

                /*获取标签*/
                $tagsRelationships = TagsRelationships::find()
                            ->select('unionid, {{%tags_relationships}}.tid')
                            ->joinWith('tag')
                            ->where('unionid = :unionid', [':unionid' => $content_id])
                            // ->createCommand()->getRawSql();
                            ->asArray()->all();
                // echo $this->app_id . '-' .  $content_id;
                // P($tagsRelationships);

                $data = array(
                    'content' => $content,
                    'tags' => $tagsRelationships
                );

                /* 增加阅读量 */
                $isMobile = Tool::isMobile();//判断是否手机访问
                if($isMobile){
                    Content::updateAllCounters(['mobile_preview_count' => 1], 'content_id = :id', [':id' => $content_id]);
                }else{
                    Content::updateAllCounters(['pc_preview_count' => 1], 'content_id = :id', [':id' => $content_id]);
                }

                // P($data);
                return Tool::showRes([0, '获取成功'], $data);
            }else{
                $data = array(
                    'content' => '',
                    'tags' => ''
                );
                // P($data);
                return Tool::showRes([0, '获取成功'], $data);
            };
        }
        return Tool::showRes(ErrMap::getError(10005));
    }

}
